package com.ruoyi.web.controller.weiriya;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.weiriya.domain.WMessage;
import com.ruoyi.weiriya.service.IWMessageService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 平台消息推送Controller
 * 
 * @author lynn
 * @date 2023-08-01
 */
@Api(tags = "平台消息推送管理")
@RestController
@RequestMapping("/weiriya/message")
public class WMessageController extends BaseController
{
    @Autowired
    private IWMessageService wMessageService;

    /**
     * 查询平台消息推送列表
     */
    @ApiOperation("获取平台消息推送列表")
    @PreAuthorize("@ss.hasPermi('weiriya:message:list')")
    @GetMapping("/list")
    public TableDataInfo list(WMessage wMessage)
    {
        startPage();
        List<WMessage> list = wMessageService.getList(wMessage);
        return getDataTable(list);
    }

    /**
     * 查询平台消息推送列表
     */
    @ApiOperation("获取平台消息推送列表")
    @PreAuthorize("@ss.hasPermi('weiriya:message:list')")
    @ApiImplicitParam(name = "id", value = "平台消息推送ID", required = true, dataType = "String", paramType = "path")
    @GetMapping("/getStateById/{id}")
    public AjaxResult getStateById(@PathVariable("id") String id)
    {
        return success(wMessageService.getStateById(id));
    }

    /**
     * 获取平台批量推送消息的子列表
     */
    @ApiOperation("获取平台批量推送消息的子列表")
    @PreAuthorize("@ss.hasPermi('weiriya:message:list')")
    @GetMapping("/listByParentId")
    public TableDataInfo listByParentId(WMessage wMessage)
    {
        startPage();
        List<WMessage> list = wMessageService.list(new LambdaQueryWrapper<WMessage>()
                .eq(WMessage::getParentId, wMessage.getParentId()));
        return getDataTable(list);
    }

    /**
     * 用户消息分类未读统计
     */
    @ApiOperation("用户消息分类未读统计")
    @PreAuthorize("@ss.hasPermi('weiriya:message:list')")
    @GetMapping("/statistical")
    public AjaxResult statistical()
    {
        List<Map<String, String>> lsit = wMessageService.statistical();
        return success(lsit);
    }


    /**
     * 导出平台消息推送列表
     */
    @ApiOperation("导出平台消息推送列表")
    @PreAuthorize("@ss.hasPermi('weiriya:message:export')")
    @Log(title = "平台消息推送", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WMessage wMessage)
    {
        List<WMessage> list = wMessageService.getList(wMessage);
        ExcelUtil<WMessage> util = new ExcelUtil<WMessage>(WMessage.class);
        util.exportExcel(response, list, "平台消息推送数据");
    }

    /**
     * 导入平台消息推送列表
     */
    @ApiOperation("导入平台消息推送列表")
    @PreAuthorize("@ss.hasPermi('weiriya:message:import')")
    @Log(title = "平台消息推送", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<WMessage> util = new ExcelUtil<WMessage>(WMessage.class);
        List<WMessage> list = util.importExcel(file.getInputStream());
        return success(wMessageService.saveBatch(list));
    }

    /**
     * 获取平台消息推送详细信息
     */
    @ApiOperation("获取平台消息推送详细")
    @ApiImplicitParam(name = "id", value = "平台消息推送ID", required = true, dataType = "String", paramType = "path")
    @PreAuthorize("@ss.hasPermi('weiriya:message:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        WMessage byId = wMessageService.getWMessageById(id);
        String openId = SecurityUtils.getLoginUser().getOpenId();
        if(StringUtils.isNotBlank(openId)){
            if(byId.getState() == 1){
                byId.setState(2);
                wMessageService.updateById(byId);
            }
        }
        return success(byId);
    }

    /**
     * 新增平台消息推送
     */
    @ApiOperation("新增平台消息推送")
    @PreAuthorize("@ss.hasPermi('weiriya:message:add')")
    @Log(title = "平台消息推送", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WMessage wMessage)
    {
        return toAjax(wMessageService.saveWMessage(wMessage));
    }

    /**
     * 修改平台消息推送
     */
    @ApiOperation("更新平台消息推送")
    @PreAuthorize("@ss.hasPermi('weiriya:message:edit')")
    @Log(title = "平台消息推送", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WMessage wMessage)
    {
        return toAjax(wMessageService.updateById(wMessage));
    }

    /**
     * 删除平台消息推送
     */
    @ApiOperation("删除平台消息推送")
    @ApiImplicitParam(name = "ids", value = "平台消息推送ID集合", required = true, dataType = "String", paramType = "path")
    @PreAuthorize("@ss.hasPermi('weiriya:message:remove')")
    @Log(title = "平台消息推送", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<String> ids)
    {
        return toAjax(wMessageService.removeByIds(ids));
    }
}
